<?php
/** 
 * EGP Framework
 *
 * LICENSE
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */

!defined('EGP_LIB_DIR') && die('Access Deny!');

/**
 * 仿 Digg 分页样式
 *
 * @package    classes
 * @author     Akon(番茄红了) <aultoale@gmail.com>
 * @copyright  Copyright (c) 2008 (http://www.tblog.com.cn)
 * @license    http://www.gnu.org/licenses/gpl.html     GPL 3
 */
class EGP_Paginator_Style_Digg extends EGP_Paginator_Style_Abstract
{

    /**
     * 获取页码的 HTML 代码
     *
     * @param  integer  $page
     * @return string
     */
    private function _getPageHtml($page)
    {
        if ($this->currentPage == $page)
            return "    <span class=\"pageCurrentItem\" title=\"当前第 $page 页\">$page</span>\n";
        else
            return "    <a href=\"" . $this->getPageLink($page) .
                   "\" class=\"pageItem\" title=\"前往第 $page 页\">$page</a>\n";
    }

    /**
     * Previous has more page
     *
     * @param  integer  $page
     * @return string
     */
    private function _getPrevMoreHtml($page)
    {
        $html = '';
        if ($page > $this->firstPage) {
            $html .= $this->_getPageHtml($this->firstPage);
            if ($page > $this->firstPage + 1)
                $html .= $this->_getPageHtml($this->firstPage + 1);
            $html .= "    <span class=\"pagePrevMore\">...</span>\n";
        }
        return $html;
    }

    /**
     * Next has more page
     *
     * @param  integer  $page
     * @return string
     */
    private function _getNextMoreHtml($page)
    {
        $html = '';
        if ($page < $this->pagesCount) {
            $html .= "    <span class=\"pageNextMore\">...</span>\n";
            if ($page < $this->pagesCount - 1)
                $html .= $this->_getPageHtml($this->pagesCount - 1);
            $html .= $this->_getPageHtml($this->pagesCount);
        }
        return $html;
    }

    /**
     * 输出分页 HTML 代码
     *
     * @return string
     */
    public function __toString()
    {
        $html = '';
        if ($this->currentPage != $this->firstPage)
            $html .= "    <a href=\"" . $this->getPageLink($this->previousPage) .
                     "\" class=\"pagePrev\" title=\"前往上一页\">&laquo; 上一页</a>\n";
        if ($this->currentPage < 6 || $this->pagesCount < 10) {
            for ($i = 1; $i < 10; $i++) {
                $html .= $this->_getPageHtml($i);
                if ($i >= $this->pagesCount)
                    break;
            }
            $html .= $this->_getNextMoreHtml($i);
        } elseif ($this->currentPage > 5 && $this->currentPage < $this->pagesCount - 4) {
            $html .= $this->_getPrevMoreHtml($this->currentPage - 4);
            for ($i = $this->currentPage - 4; $i < $this->currentPage + 5; $i++) {
                $html .= $this->_getPageHtml($i);
                if ($i >= $this->pagesCount)
                    break;
            }
            $html .= $this->_getNextMoreHtml($i);
        } elseif ($this->currentPage + 5 > $this->pagesCount) {
            $html .= $this->_getPrevMoreHtml($this->pagesCount - 8);
            for ($i = $this->pagesCount - 8; $i < $this->pagesCount + 1; $i++) {
                $i > 1 && $html .= $this->_getPageHtml($i);
            }
        }
        if ($this->itemsCount && $this->currentPage != $this->lastPage)
            $html .= "    <a href=\"" . $this->getPageLink($this->nextPage) .
                     "\" class=\"pageNext\" title=\"前往下一页\">下一页 &raquo;</a>\n";
        return "\n<div class=\"pagination\">\n$html</div>\n";
    }

}